package cn.tp.ksoft.civil_driver.module.login;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.ViewGroup;
import android.widget.ImageView;

import cn.tp.ksoft.civil_driver.DriverBaseActivity;
import cn.tp.ksoft.civil_driver.R;
import cn.tp.ksoft.civil_driver.bean.ResultBean;
import cn.tp.ksoft.civil_driver.module.MainActivity;
import cn.tp.ksoft.civil_driver.request.DriverRequest;
import cn.tp.ksoft.civil_driver.request.RequestModules;
import cn.tp.ksoft.civil_driver.utils.SystemPreference;

/**
 * Created by zhusong on 16/5/8.
 */
public class SplashActivity extends DriverBaseActivity {

    private WeakHandler mHandler;
    private DriverRequest request;
    private long startTime;

    private final int REQUEST_LOGINED = 1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ImageView bg = new ImageView(this);
        bg.setScaleType(ImageView.ScaleType.CENTER_CROP);
        bg.setImageResource(R.drawable.driver_login_background);
        bg.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        setContentView(bg);
        String token = SystemPreference.readSystemValue(this, SystemPreference.SYSTEM_VALUE_USER_TOKEN, "");
        if (TextUtils.isEmpty(token)) {
            Intent intent = new Intent(this, LoginActivity.class);
            startActivity(intent);
            finish();
            return;
        }

        mHandler = new WeakHandler(this);
        startTime = System.currentTimeMillis();
        request = new DriverRequest(this, mHandler);
        request.validateToken();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_LOGINED) {
            if (resultCode == RESULT_OK) {
                Intent intent = new Intent(this, MainActivity.class);
                startActivity(intent);
                finish();
            }
        }
    }

    @Override
    protected void handleFailureResult(int what, @NonNull ResultBean result) {
        super.handleFailureResult(what, result);
        finish();
    }

    @Override
    protected void handleSuccessResult(int what, @NonNull ResultBean result) {
        switch (what) {
            case RequestModules.VALIDATETOKEN : {
                if (result.isKickedOut()) {
                    return;
                }
                long offset = System.currentTimeMillis() - startTime;
                long delay = offset > 2000 ? 0 : 2000 - offset;
                if (delay > 0) {
                    mHandler.sendMessageDelayed(Message.obtain(mHandler, what, result), delay);
                    return;
                }

                Intent intent = new Intent(this, MainActivity.class);
                startActivity(intent);
                finish();
                break;
            }
        }
    }
}
